function openPopup(url,name,w,h) {
    var LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
    var TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
    var settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
    var  win = window.open(url,name,settings); 	
    win.focus(); 

}  
function checkSelectBoxValue(obj,fillName){
    var x = obj.value;
    if(x == ""){//index 0
        addWarningMsg('Please select '+fillName+'.');
        setFocusComboBox(obj);					
        return false;        
    }
    return true;
}
function checkBrowseBoxValue(obj,fillName){
    var x = obj.value;
    if(x == ""){//index 0
        addWarningMsg('Please browse file '+fillName+'.');
        setFocusTextBox(obj);					
        return false;        
    }
    return true;
}
function checkTextBoxValue(obj,fillName){
    //alert("ssss "+obj.value);
    var x = obj.value;
    if(x == ""){
        addWarningMsg('กรุณาใส่ข้อมูล '+fillName+' ค่ะ');
        setFocusTextBox(obj);					
        return false;        
    }
    /*
    if(x.indexOf("\'") != -1 || x.indexOf("\"") != -1){
        addWarningMsg('Invalid value \"Please don\'t use \' or \" character.\" ');
        setFocusTextBox(obj);					
        return false;
    }*/
    return true;	
}
function checkFileSupport(obj,extensionList){
    var extension = obj.value.split(".");
    //var extensionList=["jpg","gif","bmp","png"];
    var support = false;
    if(extension.length > 0){
        for(var i=0;i<extensionList.length;i++){
            if(extension[extension.length-1].toLowerCase() == extensionList[i]){
                support = true;
                break;
            }
        }
    }
    if(!support){
        addWarningMsg('Invalid file. System support only ['+extensionList+']');
        setFocusTextBox(obj);
        return false;
    }
    return true;
}
function checkBlockCharacterValue(obj){
    var x = obj.value;
    if(x.indexOf("\'") != -1 || x.indexOf("\"") != -1){
        addWarningMsg('Invalid value \"Please don\'t use \' or \" character.\" ');
        setFocusTextBox(obj);					
        return false;
    }
    return true;	
} 
function setFocusTextBox(obj){
    /*obj.style.background = '#F2D1D1';*/
    obj.focus();
}
function setFocusComboBox(obj){
    /*obj.style.background = '#F2D1D1';*/
    obj.focus();
}
function onclickTextBox(obj){
    /*obj.style.background = '#EEEEEE';
	obj.style.border ="1px solid #CBD6D7";*/
    clearWarningMsg();
    clearSystemMsg();
}
function onSubmit(obj){
    addSubmiting();
    obj.submit();
}
function addSubmiting(){
    showDiv('warningMsg');
    hideDiv('systemMsg');
    document.getElementById('warningMsg').innerHTML = '<img src=app/images/loading_small.gif />&nbsp;<font class="textProcess">Sending...</font>';
}
function addProcessing(){
    showDiv('warningMsg');
    hideDiv('systemMsg');
    document.getElementById('warningMsg').innerHTML = '<img src=app/images/loading_small.gif />&nbsp;<font class="textProcess">Processing...</font>';
}
function addProcessing(msg){
    showDiv('warningMsg');
    hideDiv('systemMsg');
    document.getElementById('warningMsg').innerHTML = '<img src=app/images/loading_small.gif />&nbsp;<font class="textProcess">'+msg+'</font>';
}
function addWarningMsg(msg){
    showDiv('warningMsg');
    hideDiv('systemMsg');    
    document.getElementById('warningMsg').innerHTML = '<img src=app/images/p_red.gif border="0"/>&nbsp;&nbsp;&nbsp;'+msg;
}
function addSysMsg(type,msg){
    hideDiv('warningMsg');
    showDiv('systemMsg');    
    if(type==0){//warning,error
        document.getElementById('systemMsg').innerHTML = '<img src=app/images/p_red.gif border="0"/>&nbsp;&nbsp;&nbsp;'+msg;
    }else{
        document.getElementById('systemMsg').innerHTML = '<img src=app/images/p_green.gif border="0"/>&nbsp;&nbsp;&nbsp;'+msg;
    }
}
function clearWarningMsg(){
    document.getElementById('warningMsg').innerHTML = '';
}
function clearSystemMsg(){
    document.getElementById('systemMsg').innerHTML = '';
}
function clearAllMsg(){
    clearWarningMsg();
    clearSystemMsg();
}
function alertMsg(msg){
    alert(msg);
}
function showDiv(id){
    document.getElementById(id).style.display = "";//show
} 
function hideDiv(id){
    document.getElementById(id).style.display = "none";
} 
 
function switchImg(divid,imgid){ 	
    if(document.getElementById(divid).style.display == "none"){
        document.getElementById(imgid).src = "img/max.gif";
    }else{
        document.getElementById(imgid).src = "img/min.gif";
    } 	
}
function switchDisplayDiv(divid){ 	
    if(document.getElementById(divid).style.display == "none"){
        document.getElementById(divid).style.display = "";
    }else{
        document.getElementById(divid).style.display = "none";
    } 	
} 


function checkSizeScreen(){
    if(fullScreen){
        hideDiv("control_panel_left");
        hideDiv("control_panel_right");
    }else{
        showDiv("control_panel_left");
        showDiv("control_panel_right");
    }
}
var fullScreen = false;
function switchSizeScreen(){ 
    switchDisplayDiv("control_panel_left");
    switchDisplayDiv("control_panel_right");
    var left = 160;
    var right = 120;
    var main = document.getElementById("body_main");
    
    
    var myScreen = screen.width-50;
    //alert(myScreen);
    //alert(myScreen-left-right);
    //alert(main.style.width);    
    if(main.style.width != AddPx(myScreen)){
        main.style.width = AddPx(myScreen);
        fullScreen = true;
    }else{
        main.style.width = AddPx(myScreen - left - right);
        fullScreen = false;
    }
    
    
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function checkBrowser(){
    //alert("x "+navigator.appName);
    return navigator.appName;
}
function rightClickOnImage(e){
    //oncontextmenu="return false;"
    if (e.which==3&& (e.target.tagName=="IMG" || e.target.tagName=="A")){//for ff
        return false;
    }	
    if (e.srcElement.tagName=="IMG" || e.srcElement.tagName=="A"){//for ie
        return false;
    }
}
function changeMsgDiv(id,msg){
    document.getElementById(id).innerHTML = msg;
}
function goUrl(url){
    window.location = url;
}
function goAction(url) {//work
    //alert("url "+url);
    document.forms[0].action = url;	
    document.forms[0].submit();
}
function goLink(url,id){//work for add parameter manual
    //alert(url);
    document.getElementById(id).href=url;	
}
function replaceAll(OldString,FindString,ReplaceString) {
    var SearchIndex = 0;
    var NewString = ""; 
    while (OldString.indexOf(FindString,SearchIndex) != -1) {
        NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
        NewString += ReplaceString;
        SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length); 
    }
    NewString += OldString.substring(SearchIndex,OldString.length);
    return NewString;
}
function jstrprintf() {
    len = arguments.length;
    if (len == 0) {
        return;
    }
    if (len == 1) {
        return arguments[0];
    }
    
    var result;
    var regexstr;
    var replstr;
    var formatstr;
    var re;
    
    result = "";
    regexstr = "";
    replstr = "";
    formatstr = arguments[0];
    
    for (var i=1; i<arguments.length; i++) {
        replstr += String(i+100) + arguments[i]  + String(i + 100);
        regexstr += String(i+100) + "(.*)" + String(i+100);
    }
    re = new RegExp(regexstr);
    var result;
    result = replstr.replace(re, formatstr);
    return result;
}

function AddPx(num) {
    return String(num) + "px";
}

function check_number() {
    e_k=event.keyCode
    //if (((e_k < 48) || (e_k > 57)) && e_k != 46 ) {
    if (e_k != 13 && (e_k < 48) || (e_k > 57)) {
        event.returnValue = false;
        alert("number only");
    }
} 

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)){
        alert("กรุณาใส่เป็นตัวเลขค่ะ");
        return false;
    }
    return true;
}
function emailValidate(obj,fillName) {
    var str = obj.value;
    if(str == ''){
        addWarningMsg('Please input '+fillName+'.');
        setFocusTextBox(obj);
        return false;
    }
     
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1){
        addWarningMsg('Invalid E-Mail format.');
        setFocusTextBox(obj);
        return false;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        addWarningMsg('Invalid E-Mail format.');
        setFocusTextBox(obj);
        return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        addWarningMsg('Invalid E-Mail format.');
        setFocusTextBox(obj);
        return false;
    }
    if (str.indexOf(at,(lat+1))!=-1){
        addWarningMsg('Invalid E-Mail format.');
        setFocusTextBox(obj);
        return false;
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        addWarningMsg('Invalid E-Mail format.');
        setFocusTextBox(obj);
        return false;
    }
    if (str.indexOf(dot,(lat+2))==-1){
        addWarningMsg('Invalid E-Mail format.');
        setFocusTextBox(obj);
        return false;
    }
    if (str.indexOf(" ")!=-1){
        addWarningMsg('Invalid E-Mail format.');
        setFocusTextBox(obj);
        return false;
    }
    clearWarningMsg();
    clearSystemMsg();
    return true;
}


function openUploadMedia(obj){
    if(!checkSelectBoxValue(obj.category,"category"))return;
    clearWarningMsg();
    clearSystemMsg();    
    window.open('uploadmediainit.do?catId='+obj.category.value+'&tbRef=&catLock=1','popup','location=0,status=0,scrollbars=1,resizable=0,width=600,height=300');
}

function refreshCaptcha(obj){
    obj.src = 'jcaptcha.jpg?'+Math.floor(Math.random()*11);
}

function validatePassword(str) {
  var ck_password = /^[A-Za-z0-9]{8,16}$/;
  return ck_password.test(str);
}

function previewImage(defaultImage){
    var x = document.forms[1].previewImage.value;
    if(x.length <= 0){
        x = defaultImage;
    }
    Tip('<img src=\''+x+'\' width=\'80\' height=\'80\' />');
}
function check_eng(e,pro) {//for one form in page
    var key =(window.event) ? event.keyCode : e.keyCode;
    alert("key "+key);
    //A-Z = 65-90
    //a-z = 97-122
    //0-9 = 48-57	
    if((key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <=122)){
        return true;
    }	
    if(!window.event){//FF
        //alert("FF");
        var v = document.forms[0].user_name.value;  	
        //alert("dddd "+v.length);	 
        if(v.length>1){	
            v = v.substring(0,v.lenght-1);
        }else{
            v = '';
        }
        document.forms[0].user_name.value = v;
    }	
    return false;
} 
//status bar for ie
function hidestatus(){
    window.status='MRTA Knowledge management Center';
    return true;
}
if (document.layers)
    document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);

document.onmouseover=hidestatus;
document.onmouseout=hidestatus;
//status bar for ie

//tool

//////////////////////////
//****************** ajax
// Start XmlHttp Object
function getXMLHttp(){
    var xmlhttp = false;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();  //firefox,other
    }
    else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   //ie
    }
    return xmlhttp;
}

function checkUsername(obj){
    var username = obj.userName.value;
    if(username != ""){
        var url = "CheckUsername?id="+username;
        var xmlhttp = getXMLHttp();
        xmlhttp.onreadystatechange=function(){
            loadCheckUsername("warningMsg",xmlhttp,obj);
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}
function loadCheckUsername(div,page_request,obj){
    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
    {
        var value = page_request.responseText;
        if(value == "NO"){
            var con = document.getElementById(div);
            con.innerHTML = jstrprintf("Username \""+obj.userName.value+"\" มีผู้ใช้งานแล้วค่ะ");
            obj.userName.value = "";
            setFocusTextBox(obj.userName);
        }else{
            clearWarningMsg();//clear
        }
    }
}
function checkCaptcha(obj){
    var code = obj.code.value;
    if(code != ""){
        var url = "CheckCaptcha?code="+code;
        var xmlhttp = getXMLHttp();
        xmlhttp.onreadystatechange=function(){
            return loadcheckCaptcha("warningMsg",xmlhttp,obj);
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}
function loadcheckCaptcha(div,page_request,obj){
    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
    {
        var value = page_request.responseText;
        if(value == "NO"){
            var con = document.getElementById(div);
            con.innerHTML = jstrprintf("รหัสไม่ถูกต้องค่ะ");
            obj.userName.value = "";
            setFocusTextBox(obj.code);
            return false;
        }else{
            clearWarningMsg();//clear
            return true;
        }
    }
}

function checkDir(obj){
    var name = obj.name.value;
    var layer = obj.layer.value;
    if(obj.id.value != ''){//edit mode
        var oldName = obj.oldName.value;
        if(oldName == name)return;
    }

    if(name != ""){
        var url = "CheckDir?name="+name+"&layer="+layer;
        var xmlhttp = getXMLHttp();
        xmlhttp.onreadystatechange=function(){
            loadCheckDir("warningMsg",xmlhttp,obj);
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}
function loadCheckDir(div,page_request,obj){
    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
    {
        var value = page_request.responseText;
        if(value == "NO"){
            var con = document.getElementById(div);
            con.innerHTML = jstrprintf("Directory \""+obj.name.value+"\" exist.");
            obj.name.value = "";
            setFocusTextBox(obj.name);
        }else{
            clearWarningMsg();//clear
        }
    }
}

function reportGetCategoryList(obj){    
    var type = obj.type.value;
    if(type != ""){
        var url = "ReportGetCategoryList?type="+type;
        var xmlhttp = getXMLHttp();
        xmlhttp.onreadystatechange=function(){
            loadReportGetCategoryList("categoryList",xmlhttp);
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}
function loadReportGetCategoryList(div,page_request){
    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
    {
        var value = page_request.responseText;
        var con = document.getElementById(div);
        con.innerHTML = jstrprintf(value);
    }
}

function jstrprintf() {
    len = arguments.length;
    if (len == 0) {
        return;
    }
    if (len == 1) {
        return arguments[0];
    }

    var result;
    var regexstr;
    var replstr;
    var formatstr;
    var re;

    result = "";
    regexstr = "";
    replstr = "";
    formatstr = arguments[0];

    for (var i=1; i<arguments.length; i++) {
        replstr += String(i+100) + arguments[i]  + String(i + 100);
        regexstr += String(i+100) + "(.*)" + String(i+100);
    }
    re = new RegExp(regexstr);
    var result;
    result = replstr.replace(re, formatstr);
    return result;
}
////////////////////////////////
